{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from glob import glob\n",
    "from tqdm import tqdm\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Images: 5822653\n"
     ]
    }
   ],
   "source": [
    "src_folder = \"/mnt/ssd2/Datasets/faces_emore_images\"\n",
    "labels_folder = \"/mnt/ssd2/Datasets/faces_emore_labels\"\n",
    "images = glob(os.path.join(src_folder, \"*.png\"))\n",
    "print(\"Images:\", len(images))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Order by label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 5822653/5822653 [34:33<00:00, 2808.51it/s]\n"
     ]
    }
   ],
   "source": [
    "labels = []\n",
    "filtered_images = []\n",
    "for imagename in tqdm(images):\n",
    "    hardlabel = np.load(imagename.replace(src_folder, labels_folder).replace('.png', '')+\"_label.npy\")\n",
    "    labels.append(int(hardlabel[0]))\n",
    "    filtered_images.append(imagename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "images = [img for _, img in sorted(zip(labels, filtered_images))]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Save to list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 706/706 [00:00<00:00, 2438.05it/s]\n"
     ]
    }
   ],
   "source": [
    "f = open(os.path.join(src_folder, \"train.lst\"),\"w\")\n",
    "for imagename in tqdm(images):\n",
    "    softlabel = np.load(imagename.replace(src_folder, labels_folder)+\"_fc1.npy\")\n",
    "    hardlabel = np.load(imagename.replace(src_folder, labels_folder).replace('.png', '')+\"_label.npy\")\n",
    "    f.write(\"1\\t\" + imagename + \"\\t\" + \n",
    "            ' '.join(map(str, np.concatenate([softlabel, hardlabel])))+\"\\n\")\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "%%sh\n",
    "cat /mnt/ssd2/Datasets/faces_emore_images/train_10classes.lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
